package com.company.二叉树;

/****
 * @author jianjian.zhang
 * @date 2022/11/06 15:26
 * @param
 * @return
 * @description:
 */

public class CreateTree {
    static int i = 0;

    public static TreeNode createBinaryTree(int[] arr) {
        if (arr == null) {
            return null;
        }

        TreeNode node = new TreeNode();
        if (i < arr.length) {
            node.data = arr[i++];
            if (node.data == 0) {
                return null;
            } else {
                node.left = createBinaryTree(arr);
                node.right = createBinaryTree(arr);
            }
        }
        return node;
    }

    public static void main(String[] args) {
        // 二叉树的值保存在数组中，以0作为分隔，数字0表示空节点，数组
        int[] arr = new int[]{1, 2, 0, 3, 4, 0, 0, 0, 5, 6, 0, 0, 7, 8, 9, 0, 0, 0, 0};
        TreeNode binaryTree = createBinaryTree(arr);
        System.out.println(binaryTree.data);
    }
}
